<template>
  <div>
    <home-header></home-header>
    <home-swiper :swiperList="swiperList"></home-swiper>
    <home-icons :iconList="iconList"></home-icons>
    <home-recommend :recommendList="recommendList"></home-recommend>
    <home-weekend :weekendList="weekendList"></home-weekend>
  </div>
</template>

<script>
  import HomeHeader from './components/Header.vue'
  import HomeSwiper from './components/Swiper.vue'
  import HomeIcons from './components/Icons.vue'
  import HomeRecommend from './components/Recommend.vue'
  import HomeWeekend from './components/Weekend.vue'
  import axios from 'axios'
  import {mapState} from 'vuex'
export default {
name:'Home',
  components:{
    HomeHeader,
    HomeSwiper,
    HomeIcons,
    HomeRecommend,
    HomeWeekend
  },
  data (){
    return {
      lastCity:'',
      swiperList:[],
      iconList:[],
      recommendList:[],
      weekendList:[]
    }
  },computed:{
    ...mapState(['city'])
  },
  methods:{
    getHomeTnfo (){
      axios.get('/api/index.json?city='+this.city)
        .then(this.getHomeInfoSucc)
    },
    getHomeInfoSucc (res){
      res = res.data
      if(res.ret&&res.data){
        const data = res.data
        this.swiperList = data.swiperList
        this.iconList = data.iconList
        this.recommendList=data.recommendList
        this.weekendList=data.weekendList
      }

    }
  },
  mounted (){//因为缓存的缘故  所以页面一上来只加载一次
    this.lastCity = this.city
    this.getHomeTnfo()
  },
  activated (){//页面每次加载  都会加载一次 是keep-alive  提供的生命周期钩子
    if(this.lastCity!==this.city){
      this.lastCity = this.city
      this.getHomeTnfo()
    }

  }
}
</script>

<style>

</style>
